import React, { PureComponent } from 'react';
import { Table, Alert } from 'antd';
import styles from './index.less';

function initTotalList(columns) {
  const totalList = [];
  columns.forEach(column => {
    if (column.needTotal) {
      totalList.push({ ...column, total: 0 });
    }
  });
  return totalList;
}

class StandardTable extends PureComponent {
  constructor(props) {
    super(props);
    const { columns } = props;
    const needTotalList = initTotalList(columns);

    this.state = {
      selectedRowKeys: [],
      needTotalList,
    };
  }

  static getDerivedStateFromProps(nextProps) {
    // componentWillReceiveProps(nextProps) {
    // clean state
    if (nextProps.selectedRows.length === 0) {
      const needTotalList = initTotalList(nextProps.columns);
      return {
        selectedRowKeys: [],
        needTotalList,
      };
    }
    if (nextProps.RowKeys) {
      return {
        selectedRowKeys: [],
      };
    }
    return null;
  }


  // 全选反选
  handleRowSelectChange = (selectedRowKeys, selectedRows) => {
    let { needTotalList } = this.state;
    const { onRowKeys } = this.props;
    needTotalList = needTotalList.map(item => ({
      ...item,
      total: selectedRows.reduce((sum, val) => sum + parseFloat(val[item.dataIndex], 10), 0),
    }));
    const { onSelectRow } = this.props;
    if (onSelectRow) {
      onSelectRow(selectedRows);
    }

    this.setState({ selectedRowKeys, needTotalList });
    onRowKeys(0);
  };

  handleTableChange = (pagination, filters, sorter) => {
    // console.log(pagination, filters, sorter)
    const { onChange } = this.props;
    if (onChange) {
      onChange(pagination, filters, sorter);
    }
  };

  cleanSelectedKeys = () => {
    this.handleRowSelectChange([], []);
  };

  render() {
    const { selectedRowKeys, needTotalList } = this.state;
    const {
      ModuleData = {},
      rowKey,
      handleModalVisible,
      setImport,
      GetExcel,
      onCloseReportk,
      operationLogVisible,
      removeHandle,
      ...rest
    } = this.props;
    const { ViewModelList = [], PageSize, PageIndex, Pages } = ModuleData;
    let { TotalRecords } = ModuleData;
    if (TotalRecords === undefined) {
      TotalRecords = 0;
    }
    const num = `共查询出 ${TotalRecords} 条记录`;
    const paginationProps = {
      showSizeChanger: true,
      showQuickJumper: true,
      // PageSize,
      // PageIndex,
      pageSize:PageSize,
      current:PageIndex,
      total: TotalRecords,
    };

    const rowSelection = {
      selectedRowKeys,
      onChange: this.handleRowSelectChange,
      getCheckboxProps: record => ({
        disabled: record.disabled,
      }),
    };

    return (
      <div className={styles.standardTable}>
        <div style={{ marginBottom: '16px' }}>
          <Alert message={num} type="info" showIcon />
        </div>
        <Table
          rowKey={(record, k) => {
            if(!record.Id || record.Id === '0'){
              return k
            }
            return record.Id
          }}
          rowSelection={rowSelection}
          dataSource={ViewModelList}
          pagination={paginationProps}
          onChange={this.handleTableChange}
          {...rest}
        />
      </div>
    );
  }
}

export default StandardTable;
